package com.moorror.codegenvel.view.window.codegen.view;

import com.google.common.collect.Sets;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionPlaces;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.moorror.codegenvel.common.select.MROneSelect;
import com.moorror.codegenvel.common.tree.actions.SQLConfigAction;
import com.moorror.codegenvel.common.tree.actions.SQLEditConfigAction;
import com.moorror.codegenvel.db.sqlite.entity.CodeGenScene;
import com.moorror.codegenvel.tab.TabJsonWrap;
import com.moorror.codegenvel.tab.TabWrapEditor;
import com.moorror.codegenvel.utils.GuiUtils;
import com.moorror.codegenvel.utils.WriteUiUtils;
import com.moorror.codegenvel.view.window.codegen.domain.TableDesc;

import javax.swing.*;
import java.awt.*;

/**
 * @Author jiyanghuang
 * @Date 2025/8/24 16:37
 */
public class TabCodeGenSceneSQL extends JPanel {

    protected JPanel toolBarPanel;

    protected JScrollPane treePanel;

    private Project project;

    private CodeGenScene codeGenScene;

    private Editor editor;

    private TabJsonWrap templateJsonValue;

    private SQLConfigAction sqlConfigAction;

    public TabCodeGenSceneSQL(Project project, CodeGenScene codeGenScene, TabJsonWrap templateJsonValue) {
        this.project = project;
        this.codeGenScene = codeGenScene;
        toolBarPanel = new JPanel();
        treePanel = new JScrollPane();
        toolBarPanel.setLayout(new BorderLayout());
        setLayout(new BorderLayout());
        add(toolBarPanel, BorderLayout.NORTH);
        add(treePanel, BorderLayout.CENTER);

        editor = TabWrapEditor.createEditorStatic("", "sql");
        treePanel.setViewportView(editor.getComponent());
        this.templateJsonValue = templateJsonValue;
        WriteUiUtils.writeEditor(this.editor, this.codeGenScene.getSqlDdl());
        installActions();
    }

    protected void installActions() {

        DefaultActionGroup actionGroup = new DefaultActionGroup("SqlGroup", false);
        if (null != ApplicationManager.getApplication()) {
            sqlConfigAction = new SQLConfigAction(this.editor, this::updateTableInfo);
            actionGroup.add(sqlConfigAction);
            actionGroup.add(new SQLEditConfigAction(this::fetchTableInfo, this::updateTableInfo, "Edit Exist SQL TableInfo"));
            actionGroup.add(new MROneSelect(Sets.newHashSet("0", "1", "2", "3", "4"), "table name prefix length split by _ ", (s) -> {
                sqlConfigAction.fillPrefixLen(s);
            }));
//            actionGroup.addSeparator();
//            actionGroup.add(new AddConfigAction());
//            actionGroup.addSeparator();
//            actionGroup.add(new DuplicateAction());
//            actionGroup.add(new SearchAction(tree));
        }

        GuiUtils.installActionGroupInToolBar(actionGroup, toolBarPanel, ActionManager.getInstance(), ActionPlaces.TOOLBAR, true);
    }

    public void fetchFromPanel(){
        codeGenScene.setSqlDdl(this.editor.getDocument().getText());
    }

    public void updateTableInfo(TableDesc tableDesc){
        codeGenScene.addTableInfo(tableDesc);
        templateJsonValue.fillSourceSelect(codeGenScene.getJsonContext());

    }


    public TableDesc fetchTableInfo(){
        codeGenScene.setJsonContext(templateJsonValue.text());
        return codeGenScene.fetchTableInfo();
    }
}
